<?php
class url extends top
{
	//重定义构造函数
    function __construct(){  
        parent::__construct();
        //定义当前导航
        $this->nav_name = 'url';
        $this->page_title = '链接管理';
        $this->breadcrumb += array('1'=>array('name'=>'链接','url'=>spUrl('url')));

    }
	function index(){	
		$data = $this->spArgs();
		//获取当前分类id
		$cat_id = isset($data['cat_id'])?$data['cat_id']:0;
		//构造左侧导航
		$cat_id = $this->__left_nav($cat_id);
		
		if(!is_numeric($cat_id)) $this->__yt_error();
		$this->left_nav_id = $cat_id;
		$this->cat_id =  $cat_id;
		//构造面包屑导航
		$category = spClass("m_category");
		$cat_info  = $category->find(array('id'=>$cat_id));
		if(empty($cat_info)) $cat_info=array('name'=>"全部",'id'=>0);
		$this->breadcrumb += array(
					'2'=>array('name'=>$cat_info['name'],'url'=>spUrl('url','index',array('cat_id'=>$cat_info['id']))),
        			'3'=>array('name'=>'列表')
        		);

		//成功添加、更新链接后的提示
		$notice = array($data['state'],$data['message']);
		
		//更新链接属性
		$url = spClass("m_url");
		if($data['url_update']){
			if(1){
				if(isset($data['weight']))				
					$data_update = array('weight'=>$data['weight']);
				elseif(isset($data['visible']))
					$data_update = array('visible'=>$data['visible']);
				else
					$this->__yt_error();

				if($url->update('id='.$data['url_id'],$data_update)){
					$notice = array('success','更新成功！');
				}else{
					$notice = array('danger','更新出错！');
				}
			}else{
				$notice = array('danger','更新有误！');
			}
		}
		//删除链接
		if($data['delete_url_id']){
			if($url->delete('id='.$data['delete_url_id'])){
				$notice = array('success','删除成功！');
			}else{
				$notice = array('danger','删除出错！');
			}
		}	
		$this->notice = $notice[0]?$notice:null;
		
		//查找链接
		
		if($data['s_submit']){
			$where = "( title like '%" .$data['s_content']. "%' OR content like '%" .$data['s_content']. "%' ) ";
			if($cat_info['id']!=0){	
				$where .= " AND cat_id='".$cat_id."'";
			}
		}else{
			if($cat_info['id']!=0){
				$where = "cat_id='".$cat_id."'";
			}else{
				$where = "";
			}
		}
		$sort= 'weight DESC,add_time DESC';
		$fields = 'id,cat_id,title,view_num,add_time,visible,weight';
		$url_result = $url->spPager($this->spArgs('page', 1), 10)->findAll($where,$sort,$fields);
		$this->pager = $url->spPager()->getPager();
		$this->url_result = $url_result;

		$this->c = 'url';
		$this->a = 'index';		
		$this->data_return = $data;		
	}
	function opera(){
		$data = $this->spArgs();
		//获取当前分类id
		$cat_id = isset($data['cat_id'])?$data['cat_id']:0;
		//构造左侧导航
		$cat_id = $this->__left_nav($cat_id);
		
		if(!is_numeric($cat_id)) $this->__yt_error();
		$this->left_nav_id = $cat_id;
		$this->cat_id =  $cat_id;
		//构造面包屑导航
		$category = spClass("m_category");
		$cat_info  = $category->find(array('id'=>$cat_id));
		if(empty($cat_info)) $cat_info=array('name'=>"全部",'id'=>0);
		$this->breadcrumb += array(
					'2'=>array('name'=>$cat_info['name'],'url'=>spUrl('url','index',array('cat_id'=>$cat_info['id']))),
					'3'=>array('name'=>"添加")
        		);

		//默认为添加链接
		$data_return = array('update_add'=>"添加",'cat_id'=>$cat_id);
		//更新链接
		$url = spClass("m_url");
		if(is_numeric($data['update_url_id'])){	
			$data_return = $url->find(array('id'=>$data['update_url_id']));
			if($data_return['cat_id']==107 ||$data_return['cat_id']==97 ||$data_return['cat_id']==98||$data_return['cat_id']==100||$data_return['cat_id']==101){
					$data_return['description'] = explode("-", $data_return['description']);
					$data_return['price'] = $data_return['description'][0];
					$data_return['description'] = $data_return['description'][1];
			}
			if(empty($data_return)) $this->__yt_error();
			$data_return['update_add'] = "更新";
			$breadcrumb = $this->breadcrumb;
			$breadcrumb['3'] = array('name'=>$data_return['title']);
			$breadcrumb['4'] = array('name'=>"更新");
			$this->breadcrumb = $breadcrumb;
		}
		//提交链接
		if($data['submit']){
			if(1){
				if($data_return['cat_id']==107 ||$data['cat_id']==97 ||$data['cat_id']==98||$data['cat_id']==100||$data['cat_id']==101){
					$data['description'] = implode("-", array($data['price'], $data['description']));
				}
				$data_insert = array(
					'editor_id'=>$_SESSION['ID'],
					'cat_id'=>$data['cat_id'],
					'title'=>$data['title'],
					'img_dir'=>$data['img_dir']?$data['img_dir']:0,
					'content'=>$data['content'],
					'description'=>$data['description'],
				);

				if($data['id']){
					$is_ok = $url->update('id='.$data['id'],$data_insert);
				}else{
					$is_ok = $url->create($data_insert);
				}
				if($is_ok){
					$this->jump(spUrl('url', 'index',array('cat_id'=>$data['cat_id'],'state'=>'success','message'=>$data['update_add'].'成功！','content_only'=>$this->content_only))); 
				}else{
					$data_return = $data;
					$this->notice = array('danger',$data['update_add'].'出错！');
				}
			}else{
				$data_return = $data;
				$this->notice = array('danger','填写有误！');				
			}
		}
		$this->data_return = $data_return;
	}	
}